/**
 * Created by star on 2015/10/29.
 */
import React from 'react';
import Immutable  from 'immutable';
import "./mi-top-B-bar-cart.less"

let Cart = React.createClass({
    getDefaultProps(){
        return {
            cartData:Immutable.List()
        }
    },
    render(){
        var totalPrice = 0;
        var cartDataHtml = [];
        var totalCount = 0;
        this.props.cartData.count() && this.props.cartData.forEach((item) => {
            item = item.toJS();
            totalCount += parseInt(item.itemAmount);
            totalPrice += (item.itemPrice * item.itemAmount)
            var imgUrl = item.itemImg ? item.itemImg :"";
            cartDataHtml.push(
                <li>
                    <div className="propic">
                        <a href={"/item/" + item.itemNo}>
                            <img src={imgUrl} width="50" height="50"/>
                        </a>
                    </div>
                    <div className="prodesc" title={item.itemName}>
                        <h3>{item.itemName}</h3>
                    </div>
                    <div className="buy_num">
                        <p className="">
                            <i className="price">{parseFloat(item.itemPrice).toFixed(2)}</i><i>*</i><i className="num">{item.itemAmount}</i>
                        </p>
                        <a href="javascript:;" className="delete" onClick={this._delCart.bind(this, item.itemNo)} >删除</a>
                    </div>
                </li>
            )
        });

        return (
            cartDataHtml.length > 0 ?  <div className="mi-cart cart_body">
                <ul>
                    {cartDataHtml}
                </ul>
                <div className="cart_bottom">
                    <p>{'共' + totalCount + '件商品'}</p>
                    <a className="jiesuan" href="/cart">结&nbsp;算</a>
                    <span className="price_all">&yen;{totalPrice.toFixed(2)}</span>
                </div>
            </div> :<div className="cart_body_null"><h3>购物车中还没有商品,再去逛逛吧!</h3></div>
        )
    },
    _delCart(goodsId) {
        $.post('/cart/remove_good', {itemIds: goodsId}, function(json) {
            if (json.status == 1) {
                window.XSite_Info.initCart();
            }
        })
    }

});
module.exports = Cart;